/** Author: David Powell
This file takes the raw Open Payments Data and constructs the relevant measures by state-year
I downloaded the original data files June 16, 2017
**/

clear all
set more off


global dir "/jules/b/dpowell/"
global RDATA "${dir}/purdue/replication/rawdata/openpayments"
global SDATA "${dir}/purdue/replication/data_cleaning/cleaned"




foreach nnn of numlist 2013/2015 {
	clear
	import delimited using ${RDATA}/OP_DTL_GNRL_PGYR`nnn'_P06302017.csv
	gen temp=lower(name_of_associated_covered_drug)
	gen mark=0
	replace mark=1 if index(temp, "oxycontin")>0
	gen totaloxy=total_amount*mark

	gcollapse (sum) total_amount mark totaloxy, by(rec*state program_year) fast
	save ${SDATA}/general_purdue`nnn', replace
}


foreach nnn of numlist 2016 {
	clear
	import delimited using ${RDATA}/OP_DTL_GNRL_PGYR`nnn'_P06302017.csv

	gen temp=lower(name_of_dr)
	gen mark=0
	replace mark=1 if index(temp, "oxycontin")>0
	gen totaloxy=total_amount*mark

	gcollapse (sum) total_amount mark totaloxy, by(rec*state program_year) fast
	save ${SDATA}/general_purdue`nnn', replace
}


use ${SDATA}/general_purdue2013
for num 2014/2016: append using ${SDATA}/general_purdueX
fillin rec program_year 
replace total_amount=0 if _fil==1
replace totaloxy=0 if _fil==1
drop _fil
rename program year

statastates, abbrev(rec)
keep if _m==3
rename state_fips stfips


bys stf year: keep if _n==1
keep stf year totaloxy total_amount
sort stf year


rename totaloxy oxypayments
rename total_amount totalpayments 

save ${SDATA}/openpayments, replace









